import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		loginState: !!uni.getStorageSync('loginState'),
		userInfo: !!uni.getStorageSync('userInfo') ?
			JSON.parse(uni.getStorageSync('userInfo')) :
			{
				name: '未知用户',
				employee_id: '未知工号'
			}
	},
	mutations: {
		userLogin(state, userInfo) {
			state.loginState = true;
			state.userInfo = userInfo;
			uni.setStorageSync('loginState', 'ok');
			uni.setStorageSync('userInfo', JSON.stringify(userInfo));
		},
		userLogout(state) {
			state.loginState = false
			state.userInfo = {
				name: '未知用户',
				employee_id: '未知工号'
			}
			uni.clearStorageSync('userInfo')
			uni.clearStorageSync('loginState')
		}
	},
	actions: {
		// 在这里定义触发 mutations 的异步方法
		userLoginAction(context, userInfo) {
			context.commit('userLogin', userInfo)
		},
		userLogoutAction(context) {
			context.commit('userLogout')
		}
	},
	modules: {
		// 在这里定义子模块
	}
})

export default store